<?php
	//Start session
	require_once("bootstrap.php");
	//database for pdo
	global $db;
	//Array to store validation errors
	$errmsg_arr = array();
	//Validation error flag
	$errflag = false;

function output($custArray) {
	//START FORMATTING STRING; WRAP CONTENT TAG AROUND ENTIRE MESSAGE
	$outputString = ''; //start empty
	$outputString .= "<?xml version=\"1.0\"?>\n";
	$outputString .= "<content>\n";
	
	if($_GET['string'] == 'orderDates'){
		$custCount = 0;
		$customerIDs = $_SESSION['SESS_CUSTOMER_ID'];
		while($custCount < count($customerIDs) ){
			$thisID = $customerIDs[$custCount];
			$theseDates = $custArray[$thisID];
			$outputString .= "<DateGroup>";
			$outputString .= "<CustID>" . $thisID . "</CustID>";
			$outputString .= "<DateCount>" . count($theseDates) . "</DateCount>";
			$dateCount = 0;
			while($dateCount < count($theseDates)){
				$outputString .= "<Date>" . $theseDates[$dateCount] . "</Date>";
				$dateCount++;
			}
			$outputString .= "</DateGroup>";
			$custCount++;
		}
	} else if($_GET['string'] == 'orderDetailInfoPerDay'){
		$custCount = 0;
		$customerIDs = $_SESSION['SESS_CUSTOMER_ID'];
		while($custCount < count($customerIDs) ){
			$thisID = $customerIDs[$custCount];
			$detailsForID = $custArray[$thisID];
			$outputString .= "<DetailsGroup>";
			$outputString .= "<CustID>" . $thisID . "</CustID>";
			$outputString .= "<DetailsCount>" . count($detailsForID) . "</DetailsCount>";
			$DetailCount = 0;
			while($DetailCount < count($detailsForID)){
				$outputString .= "<Details>";			
				$outputString .= "<UPC>" . $detailsForID[$DetailCount] ['UPC']  . "</UPC>";
				$outputString .= "<ITEM_QUANTITY>" . $detailsForID[$DetailCount] ['ITEM_QUANTITY'] . "</ITEM_QUANTITY>";
				$outputString .= "<TOTAL_RETAIL_AMOUNT>" .$detailsForID[$DetailCount] ['TOTAL_RETAIL_AMOUNT'] . "</TOTAL_RETAIL_AMOUNT>";
				$outputString .= "<TOTAL_DISCOUNT_AMOUNT>" . $detailsForID[$DetailCount] ['TOTAL_DISCOUNT_AMOUNT'] . "</TOTAL_DISCOUNT_AMOUNT>";
				$outputString .= "<DEPARTMENT>" . $detailsForID[$DetailCount] ['DEPARTMENT'] . "</DEPARTMENT>";
				$outputString .= "<CATEGORY>" . $detailsForID[$DetailCount] ['CATEGORY'] . "</CATEGORY>";
				$outputString .= "<HEB_BRAND_FLAG>" . $detailsForID[$DetailCount] ['HEB_BRAND_FLAG'] . "</HEB_BRAND_FLAG>";
				$outputString .= "<COMPARABLE_NATIONAL>" . $detailsForID[$DetailCount] ['COMPARABLE_NATIONAL'] . "</COMPARABLE_NATIONAL>";				
				$outputString .= "<HEALTHY_LIVING_FLAG>" . $detailsForID[$DetailCount] ['HEALTHY_LIVING_FLAG'] . "</HEALTHY_LIVING_FLAG>";			
				$outputString .= "</Details>";
				$DetailCount++;
			}
			$outputString .= "</DetailsGroup>";
			
			$custCount++;
		}
	}
	$outputString .= "</content>";	
	return $outputString;	
}
	
function outputErrors($errNum, $errMsgArr) {
	$ct = 0;
	$outputString = ''; //start empty
	$outputString .= "<?xml version=\"1.0\"?>\n";
	$outputString .= "<content>\n";
	$outputString .= "<errNum>" . $errNum . "</errNum>\n";
	while($ct < $errNum){
		$outputString .="<ERROR>" . $errMsgArr[$ct] . "</ERROR>\n";
		$ct += 1;
	}
	
	$outputString .= "</content>";	
	return $outputString;	
}
	function doService(){
		
		global $db;
		
		if(!isset($_GET['string']) || $_GET['string'] ==''){
			$errMsgArr[] = 'String Missing';
			$errNum += 1;
			return "ERROR";
		} else {
			
			if($_GET['string'] == 'orderDates'){
				$custDateArray = array();
				$currCustomer = 0;
				$customerIDs = $_SESSION['SESS_CUSTOMER_ID'];
				while($currCustomer < count($customerIDs) ){
					$myDates = array();
					$thisID = $customerIDs[$currCustomer];
					$prep = $db->prepare("SELECT DISTINCT ORDER_DATE
									FROM `order_header`
									WHERE ORDER_ID IN (SELECT ORDER_ID 
												      FROM `customer_order`
												      WHERE CUSTOMER_ID=:id) ORDER BY ORDER_DATE; ");
					if ( $prep->execute(array( ":id" =>  $thisID ))) {
						$ct = 0;
						$custDateArray[$thisID] = array();
						while(($info = $prep->fetch(PDO::FETCH_ASSOC))){
							$myDates[$ct] = $info['ORDER_DATE'];
							$ct++;
						}
						$custDateArray[$thisID] = $myDates;
					} else {
						$error = $prep->errorInfo();
						$errMsgArr[] = $error[2];
						$errNum += 1;	
						return outputErrors($errNum, $errMsgArr);
					}
					$currCustomer++;					
				}
				return output($custDateArray);
			}
			
			if($_GET['string'] == 'orderDetailInfoPerDay'){
				$custDetailForDate = array();
				$currCustomer =  intval($_GET['selectIndex']);
				$customerIDs = $_SESSION['SESS_CUSTOMER_ID'];
				$myDate = $_GET['date'];
				//while($currCustomer < count($customerIDs) ){
					$myDetails = array();
					$thisID = $customerIDs[$currCustomer];
					$prep = $db->prepare("SELECT orderDetail.UPC, 
										   orderDetail.ITEM_QUANTITY, 
										   orderDetail.TOTAL_RETAIL_AMOUNT, 
										   orderDetail.TOTAL_DISCOUNT_AMOUNT,
										   product.DEPARTMENT, 
										   product.CATEGORY, 
										   product.HEB_BRAND_FLAG, 
										   product.HEALTHY_LIVING_FLAG
									FROM `order_detail` as orderDetail, 
										`product` as product
									WHERE product.UPC=orderDetail.UPC AND ORDER_DATE=:myDate 
											AND orderDetail.ORDER_ID IN (SELECT ORDER_ID 
																FROM `customer_order`
																WHERE CUSTOMER_ID=:id) ;");
					if ( $prep->execute(array( ":id" =>  $thisID, ":myDate" => $myDate ))) {
						$ct = 0;
						$custDetailForDate[$thisID] = array();
						while(($info = $prep->fetch(PDO::FETCH_ASSOC))){
						
							$nationalCompFlagPrep = $db->prepare("SELECT count(*)
											FROM `heb_brand_national_brand`
											WHERE NATIONAL_BRAND_UPC=:myUPC ;");
							if ( $nationalCompFlagPrep->execute(array( ":myUPC" =>  $info['UPC']))) {
								while(($compFlag = $nationalCompFlagPrep->fetch(PDO::FETCH_ASSOC))){
									$info['COMPARABLE_NATIONAL'] = $compFlag['count(*)'];
								}
							} else {
								$info['COMPARABLE_NATIONAL'] = 0;
							}
						
							
							$myDetails[$ct] = $info;
							$ct++;
						}
						$custDetailForDate[$thisID] = $myDetails;
					} else {
						$error = $prep->errorInfo();
						$errMsgArr[] = $error[2];
						$errNum += 1;	
						return outputErrors($errNum, $errMsgArr);
					}
				
				return output($custDetailForDate);
			}
			
			
		
		
	
		}	
	
	}
	
	
	
	
	$myOutput = doService();
	
	print($myOutput);

?>
